import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router"
import VueResource from "vue-resource"
import VueImg from 'v-img'
import $ from 'jquery'
import routes from './router/index'
import Config from './config.js'
import MintUI from 'mint-ui'
import VueAwesomeSwiper from 'vue-awesome-swiper'

import VueClipboard from 'vue-clipboard2'
VueClipboard.config.autoSetContainer = true
Vue.use(VueClipboard);


//开启debug模式

Vue.config.debug = true;
Vue.config.devtools = true;
if (process.env.NODE_ENV == 'local') {
	Vue.config.devtools = true;
} else {
	Vue.config.devtools = false;
}
Vue.use(VueImg);
Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(MintUI);
Vue.prototype.utilHelper = Config;
Vue.use(VueAwesomeSwiper)
const router = new VueRouter({
	base: __dirname,
	routes
});
Vue.directive('title', {
    inserted: function (el, binding) {
        document.title = el.dataset.title
    }
});
Vue.directive('isShow',{//判断用户是否登录，如果没登录就不显示该标签
	inserted:function(el,binding){
		let userId = window.localStorage.getItem("3_userId");
		if(!userId){
			el.style.display='none';
		//	return false;
		}else{
			el.style.visibility='visible';
		//	return true;
		}
	}
});
Vue.directive('login',{//判断用户是否登录，如果没登录就不显示该标签
	inserted:function(el,binding){
		let userId = window.localStorage.getItem("3_userId");
		if(!userId){
			el.style.top = '60px';
		//	return false;
		}else{
			el.style.top = '240px';
		//	return true;
		}
	}
});
Vue.prototype.tourist = function (){
    //若为游客则弹出绑定对话框
    var userRole =  window.localStorage.getItem("3_userRole");
    var userId =  window.localStorage.getItem("3_userId");
    if(!userRole||!userId){
        router.push({path:'/login'});
        return false;
	}
    if(userRole && userRole == '-1'){
        layer.confirm('您尚未绑定公司！',{
                btn :['以后再说','去绑定'],
                title: false,
                closeBtn: 0, //不显示关闭按钮
                shade: [0]
            },function(index){
                layer.close(index);
                return true;
			},
            function(index){
                router.push({path:'/bindCompany'});
                layer.close(index);
                return false;
            }
        );
    }
    return true;
}
router.beforeEach(({meta,path}, from, next) => {
	console.info(path+"####");
	//当返回到顶层时,将当前路径设置为下一个返回路径
	var isLogin = window.localStorage["3_isLogin"];
	if(path.indexOf("/declareDetail") == 0 && path.indexOf("/templateMessage") > 0 && !isLogin) {
		return next({
			path: '/login?redirectUrl='+path
		});
	}
    if("/register" == path || "/forgetPassword" == path || path.indexOf("/sharePremisesDetail") == 0){
        return next();
    }
    if("/index" == path || "/" == path || "/app" == path || "/login" == path){
		//当返回至 /app 时,往前走一步,跳转到首页
		if("/app" == path){
			window.history.forward(1);
		}
		if("/login" == path && (isLogin==true || isLogin=="true")){
			return next({
				path: '/index'
			});
		}
		history.pushState(null, null, document.URL);
	}

	/*if((!isLogin || isLogin == 'null' || isLogin==false || isLogin=="false")&& path !== '/login') {
		return next({
			path: '/login'
		})
	}*/
	next()
	
});

Vue.http.interceptors.push((request, next) => {
	let companyUuid = getQueryString('companyUuid')
	//绑定登陆ID
	var userId=window.localStorage.getItem("3_userId");
	var requestBody=request.getBody();
	if (requestBody && requestBody.params) {
		if (userId && request.url.indexOf("getLogo") == -1 && request.url.indexOf("login") == -1) {
            requestBody.params.userId=userId;
		}
		requestBody.params.loginType = "3";
		request.body=requestBody;
	}
	if(!request.url.includes('://')) {
		request.url = Config.apiUrl + request.url
	}
	request.headers.set('loginType','3');
	request.headers.set('userId',userId);
	request.headers.set('companyUuid', companyUuid);
		next((response) => {
		//验证登陆
		var isLogin = window.localStorage["3_isLogin"];
		if(!response.ok && isLogin == "true"){
			window.localStorage.removeItem("3_isLogin");
			window.localStorage.removeItem("3_userId");
			router.replace({
				path: '/login'
			});
			return;
		}

		if(isLogin == "true") {
			if(typeof(response.body) == 'string') {
				var responseBody = JSON.parse(response.body);
				var result = responseBody.result;
				if(result && result == "-1") {
                    window.localStorage["3_isLogin"] = null;
					router.replace({
						path: '/login'
					});
					return;
				}
			}else if(typeof(response.body) == 'object'){
				var result=response.body.result;
				if(result && result == "-1") {
                    window.localStorage["3_isLogin"] = null;
					router.replace({
						path: '/login'
					});
					return;
				}
			}
		}
	});
});



new Vue({
	el: "#app",
	router,
	render: r => r(App)
})

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2])
	};
	return null
}





